package com.xizimall.portal.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xizimall.pojo.TbUser;
import com.xizimall.portal.pojo.CartItem;
import com.xizimall.portal.pojo.Order;
import com.xizimall.portal.service.CartService;
import com.xizimall.portal.service.OrderService;
/**
 * 订单服务
 * <p>Title: OrderController</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年5月2日下午6:49:58
 * @version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private CartService cartService;
	
	@Autowired
	private OrderService orderService;
	
    @RequestMapping("/order-cart")
    public String showOrderCart(HttpServletRequest request,HttpServletResponse response,Model model){
    	List<CartItem> list = cartService.getCartItemList(request, response);
    	model.addAttribute("cartList", list);
    	return "order-cart";
    }
    @RequestMapping("/create")
    public String createOrder(Order order,Model model,HttpServletRequest request){
     try {
    	 //从request中取用户信息
    	 TbUser user = (TbUser) request.getAttribute("user");
    	 //在order中补全用户对象
    	 order.setUserId(user.getId());
    	 order.setBuyerNick(user.getUsername());
    	 String orderId = orderService.createOrder(order);
         model.addAttribute("orderId", orderId);
         model.addAttribute("payment", order.getPayment());
         model.addAttribute("date", new DateTime().plus(3).toString("yyyy-MM-dd"));
         return "success";
	} catch (Exception e) {
		e.printStackTrace();
		model.addAttribute("massage", "创建订单出错,请稍候重试");
		return "error/exception";
	}
     }
}
